# Python 环境配置
# 这个文件包含所有 Python 相关的配置

# Miniconda路径
export PATH="$HOME/miniconda3/bin:$PATH"

# 初始化conda
if [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then
    . "$HOME/miniconda3/etc/profile.d/conda.sh"
else
    export PATH="$HOME/miniconda3/bin:$PATH"
fi

# pipx 配置 (用于安装全局 Python 应用)
export PATH="$HOME/.local/bin:$PATH"

# 使用国内镜像源
export CONDA_CHANNEL_PRIORITY=flexible
cat > ~/.condarc << EOF
channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
EOF

# pip国内镜像
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF

# 虚拟环境管理函数
function conda-create() {
  local env_name="${1:-myenv}"
  local python_version="${2:-3.9}"
  conda create -n "$env_name" python="$python_version" -y
  log_success "已创建conda环境: $env_name (Python $python_version)"
}

function conda-remove() {
  local env_name="${1:-myenv}"
  conda remove -n "$env_name" --all -y
  log_success "已删除conda环境: $env_name"
}

function conda-list() {
  log_header "已安装的conda环境"
  conda env list
}

# Python 常用别名
alias py="python"
alias pip-upgrade="pip install --upgrade pip"
alias pip-list="pip list --outdated"
alias conda-info="conda info"
alias conda-envs="conda env list"

# 检查 Python 环境函数
function check_python_env() {
  log_header "Python 环境信息"
  log_python "$(python --version 2>&1)"
  log_path "Python 路径" "$(which python)"
  log_version "pip" "$(pip --version 2>&1)"
  
  log_subheader "Conda 信息"
  conda info
  
  log_subheader "已安装的conda环境"
  conda env list
  
  if [ -n "$CONDA_DEFAULT_ENV" ] && [ "$CONDA_DEFAULT_ENV" != "base" ]; then
    log_info "当前激活的环境: $CONDA_DEFAULT_ENV"
  fi
}

# 别名
alias py-env="check_python_env"
alias py-create="conda-create"
alias py-remove="conda-remove"
alias py-list="conda-list"
alias py-paths="log_python_paths"

# 当加载此文件时显示 Python 版本
log_python "$(python --version 2>&1)" 